<template>
  <div class="login-container">
    <div class="logo" />
    <div class="form">
      <h1>登录</h1>
      <el-card shadow="never" class="login-card">
        <!--登录表单-->
        <!-- 
          element-ui想要让表单有校验功能，需要做以下4件事：
          1. el-form：model表单数据对象
          2. el-form：rules表单校验规则对象
          3. el-form-item：prop指定表单项的校验字段
          4. 在表单项上绑定 v-model="表单数据对象.字段"
        -->
        <el-form ref="loginRef" :model="loginForm" :rules="loginRules">
          <el-form-item prop="mobile">
            <el-input
              v-model="loginForm.mobile"
              placeholder="请输入手机号"
            ></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input
              v-model="loginForm.password"
              show-password
              placeholder="请输入密码"
            ></el-input>
          </el-form-item>
          <el-form-item prop="isAgree">
            <el-checkbox v-model="loginForm.isAgree"
              >用户平台使用协议</el-checkbox
            >
          </el-form-item>
          <el-form-item>
            <el-button type="primary" style="width: 350px" @click="login"
              >登录</el-button
            >
          </el-form-item>
        </el-form>

        <!-- <el-button type="primary" style="width: 350px" @click="loginText">
          登录测试
        </el-button> -->
      </el-card>
    </div>
  </div>
</template>
<script>
// import axios from "axios";
// import request from "@/utils/request";
export default {
  name: "Login",
  data() {
    return {
      // 表单数据对象
      loginForm: {
        mobile: process.env.NODE_ENV === "development" ? "13800000002" : "",
        password: process.env.NODE_ENV === "development" ? "hm#qd@23!" : "",
        isAgree: process.env.NODE_ENV === "development" ? true : false,
      },
      // 表单验证规则对象
      loginRules: {
        mobile: [
          // 必填校验
          { required: true, message: "请输入手机号", trigger: "blur" },
          // 格式校验
          {
            pattern: /^1[3-9]\d{9}$/,
            message: "请输入正确手机号",
            trigger: "blur",
          },
        ],
        password: [
          // 必填校验
          { required: true, message: "请输入密码", trigger: "blur" },
          // 长度校验
          { min: 6, max: 16, message: "请输入6-16位的密码", trigger: "blur" },
        ],
        isAgree: [
          // required: true 必填校验只能校验string或number类型的表单的值
          // 自定义校验规则
          {
            // 自定义校验规则函数
            validator: (rule, value, callback) => {
              // rule: 规则信息
              // value: 该校验规则的表单数据值
              // callback: 是一个函数
              //   1.如果校验通过：callback()
              //   2.如果校验不通过：callback(new Error('提示给用户的错误信息'))
              if (value) {
                // 校验通过
                callback();
              } else {
                // 校验不通过
                callback(new Error("请勾选用户平台使用协议"));
              }
            },
            trigger: "change",
          },
        ],
      },
    };
  },
  methods: {
    // 登录
    login() {
      // 兜底校验（表单的整体校验）
      this.$refs.loginRef.validate(async (isOK) => {
        if (isOK) {
          // 调用vuex中user模块下的action函数
          await this.$store.dispatch("user/login", this.loginForm);
          // 跳转到首页
          this.$router.push("/");
        }
      });
    },
    // // 登录测试
    // async loginText() {
    //   // 发送登录请求
    //   await request({
    //     url: "/sys/login",
    //     method: "post",
    //     data: {
    //       mobile: "13800000002",
    //       password: "123456",
    //     },
    //   });
    // },
  },
};
</script>
<style lang="scss">
.login-container {
  display: flex;
  align-items: stretch;
  height: 100vh;
  .logo {
    flex: 3;
    background: rgba(38, 72, 176) url(../../assets/common/login_back.png)
      no-repeat center / cover;
    border-top-right-radius: 60px;
    display: flex;
    flex-direction: column;
    align-items: flex-end;
    justify-content: center;
    padding: 0 100px;
    .icon {
      background: url(../../assets/common/logo.png) no-repeat 70px center /
        contain;
      width: 300px;
      height: 50px;
      margin-bottom: 50px;
    }
    p {
      color: #fff;
      font-size: 18px;
      margin-top: 20px;
      width: 300px;
      text-align: center;
    }
  }
  .form {
    flex: 2;
    display: flex;
    flex-direction: column;
    justify-content: center;
    padding-left: 176px;
    .el-card {
      border: none;
      padding: 0;
    }
    h1 {
      padding-left: 20px;
      font-size: 24px;
    }
    .el-input {
      width: 350px;
      height: 44px;
      .el-input__inner {
        background: #f4f5fb;
      }
    }
    .el-checkbox {
      color: #606266;
    }
  }
}
</style>
